package org.gdsdxy.pmmp.web.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import org.gdsdxy.pmmp.entity.MemberBaseInfo;
import org.gdsdxy.pmmp.page.PageConfig;
import org.gdsdxy.pmmp.page.PageResult;
import org.gdsdxy.pmmp.service.IUserManagerService;


public class MemberImageAction extends DispatchAction {
  private IUserManagerService service;

  public void setService(IUserManagerService service) {
    this.service = service;
  }

	
	public ActionForward query(ActionMapping mapping,ActionForm form,HttpServletRequest req,HttpServletResponse resp){
		MemberBaseInfo mbi = (MemberBaseInfo)req.getSession().getAttribute("member");
		if(mbi==null){
			return mapping.findForward("login");
		}
		return this.first(mapping, form, req, resp);
	}
	public ActionForward delete(ActionMapping mapping,ActionForm form,HttpServletRequest req,HttpServletResponse resp){
		System.out.println("ɾ��");
		return null;
	}

	
	public ActionForward update(ActionMapping mapping,ActionForm form,HttpServletRequest req,HttpServletResponse resp){
		System.out.println("����");
		return null;
	}
	
	public ActionForward first(ActionMapping mapping,ActionForm form,HttpServletRequest req,HttpServletResponse resp){
		System.out.println("first");
		PageConfig pageConfig = (PageConfig) req.getSession().getAttribute("page");
		if(pageConfig==null){
			pageConfig = new PageConfig(1,2);
			req.getSession().setAttribute("page", pageConfig);
		}
		MemberBaseInfo mbi = (MemberBaseInfo)req.getSession().getAttribute("member");
		if(mbi==null){
			return mapping.findForward("login");
		}
		pageConfig.setCurrentPageNo(1);
		int start= pageConfig.getPerRecordNo()*(pageConfig.getCurrentPageNo() - 1);
		int size=pageConfig.getPerRecordNo();
		PageResult imgeList=service.queryImgeByUserIdFy(start, size, mbi.getID());
		pageConfig.setAllRecordNum( Long.valueOf(imgeList.getTotalRecord()).intValue());
		req.getSession().setAttribute("page", pageConfig);
		req.setAttribute("imageList", imgeList);
		return mapping.findForward("imgelist");
	}
	
	public ActionForward previous(ActionMapping mapping,ActionForm form,HttpServletRequest req,HttpServletResponse resp){
		System.out.println("previous");
		PageConfig pageConfig = (PageConfig) req.getSession().getAttribute("page");
		MemberBaseInfo mbi = (MemberBaseInfo)req.getSession().getAttribute("member");
		if(mbi==null){
			return mapping.findForward("login");
		}
		if(pageConfig.getCurrentPageNo()-1==0){
			
		}else{
			pageConfig.setCurrentPageNo(pageConfig.getCurrentPageNo()-1);
			
		}
		int start=0;
		if(pageConfig.getCurrentPageNo()>0){
			start = pageConfig.getPerRecordNo()
			 			*(pageConfig.getCurrentPageNo()-1);	
		}
		int size=pageConfig.getPerRecordNo();
		PageResult imgeList=service.queryImgeByUserIdFy(start, size, mbi.getID());
		pageConfig.setAllRecordNum( Long.valueOf(imgeList.getTotalRecord()).intValue());
		req.getSession().setAttribute("page", pageConfig);
		req.setAttribute("imageList", imgeList);
		return mapping.findForward("imgelist");
	}
	
	public ActionForward next(ActionMapping mapping,ActionForm form,HttpServletRequest req,HttpServletResponse resp){
		System.out.println("next");
		PageConfig pageConfig = (PageConfig) req.getSession().getAttribute("page");
		MemberBaseInfo mbi = (MemberBaseInfo)req.getSession().getAttribute("member");
		if(mbi==null){
			return mapping.findForward("login");
		}
		if(pageConfig.getTotalPageNo()<pageConfig.getCurrentPageNo()+1){
			
		}
		else{
			pageConfig.setCurrentPageNo(pageConfig.getCurrentPageNo()+1);
		}
		int start=pageConfig.getPerRecordNo()*(pageConfig.getCurrentPageNo()-1);
		int size=pageConfig.getPerRecordNo();
		PageResult imgeList=service.queryImgeByUserIdFy(start, size, mbi.getID());
		pageConfig.setAllRecordNum( Long.valueOf(imgeList.getTotalRecord()).intValue());
		req.getSession().setAttribute("page", pageConfig);
		req.setAttribute("imageList", imgeList);
		return mapping.findForward("imgelist");
	}
	
	
	public ActionForward last(ActionMapping mapping,ActionForm form,HttpServletRequest req,HttpServletResponse resp){
		System.out.println("last");
		PageConfig pageConfig = (PageConfig) req.getSession().getAttribute("page");

		MemberBaseInfo mbi = (MemberBaseInfo)req.getSession().getAttribute("member");
		if(mbi==null){
			return mapping.findForward("login");
		}
	    pageConfig.setCurrentPageNo(pageConfig.getTotalPageNo());
	    int start=pageConfig.getPerRecordNo()*(pageConfig.getCurrentPageNo()-1);
	    int size=pageConfig.getPerRecordNo();
	    PageResult imgeList=service.queryImgeByUserIdFy(start, size, mbi.getID());
	    pageConfig.setAllRecordNum( Long.valueOf(imgeList.getTotalRecord()).intValue());
	    req.getSession().setAttribute("page", pageConfig);
		req.setAttribute("imageList", imgeList);
		return mapping.findForward("imgelist");
	}
}
